package cn.sumpu.app.guoguojie.domain.Ext;

import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

public class Expression implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2283040604180673959L;
	/**
	 * 
	 */
	public Expression(){
		//初始化
		expressionMap = cn.sumpu.app.guoguojie.constant.Expression.cache;
	}
	
	private Map<String, String> expressionMap = new HashMap<String, String>();
	
	public Map<String, String> getExpressionMap() {
		return expressionMap;
	}

	public void setExpressionMap(Map<String, String> expressionMap) {
		this.expressionMap = expressionMap;
	}
	
	//设置表情
	public void putExpression(HttpServletRequest request){
		try{
			//动态取得文件夹里面的表情的名字
			File file = new File(request.getSession(true).getServletContext().getRealPath("")+"/images/expression");
			if(file != null){
				String exp[] = file.list();
				if(exp!=null && exp.length>0){
					for(String str:exp){
						if(str.length()<=4)
							continue;
						String a = str.substring(0,str.lastIndexOf("."));
						expressionMap.put(str, "/"+a);
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//替换表情字符
	public static String replaceAll(String content){
		Expression e = new Expression();
		if(content == null)
			return "";
		content = content.replace("<", "&lt;");
		Iterator<Entry<String, String>> it = e.expressionMap.entrySet().iterator();
		while (it.hasNext()) {
		    Entry<String, String> entry = it.next();
		    String key = entry.getKey();
		    String value = entry.getValue();
		    content = content.replace((CharSequence) value, "<img src='images/expression/"+key+"' />");
		}
		return content;
	}
	
}
